package java_thinking.unit_18_IO._18_1;

import java_thinking.net.mindview.util.TextFile;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @Desc:   修改DirList.java,以便FilenameFilter能够打开每个文件，并检查命令行尾随的参数是否存在于那个文件中，
 *          以此检查结果来决定是否接受这个文件。
 * @author: cww
 * @DateTime: 2019-05-12 11:02
 */

public class E01_SearchWords {
    public static void main(String[] args) {
        File path = new File(".");
        String[] list;
        list = path.list(new FilenameFilter() {
            private String ext = ("txt").toLowerCase();
            @Override
            public boolean accept(File dir, String name) {
                if (name.toLowerCase().endsWith(ext)) {
                    //找出文件内部所有单词
                    Set<String> words = new HashSet<>(new TextFile(new File(dir,name).getAbsolutePath(),"\\W+"));
                    String[] wordsContain = {"util", "public"};
                    for (int i = 1; i < wordsContain.length; i++) {
                        if (words.contains(wordsContain[i])) {
                            return true;
                        }
                    }
                }
                return false;
            }
        });
        Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);
        for (String s : list) {
            System.out.println(s);
        }
    }
}
